

export const init = (self, tiyData) => {
  self.$ySpliceAll(tiyData.tData.websiteList, tiyData.tData.customerList);
  tiyData.tParams.website = "";
  tiyData.tParams.isOnlyInfoed = false;
  self.$ySpliceAll(tiyData.tParams.selectedUsers, tiyData.tParams.selectedUrls);
  tiyData.tPage.total = 1;
  tiyData.tPage.startIndex = 0;
  tiyData.tPage.stepNumber = 20;
  tiyData.tPage.currentPage = 1;
};

/**
 * 从服务器请求列表
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const searchService = (self, tiyData) => {
  self.$Spin.show();

  self.$apiPlugin.findSameWebsite({
    "pageSize": tiyData.tPage.stepNumber,
    "pageNum": tiyData.tPage.currentPage
  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      let dataPage = response.result;
      tiyData.tPage.total = dataPage.total;
      tiyData.tData.websiteList.splice(0, tiyData.tData.websiteList.length);
      dataPage.list.forEach(
        function (item) {
          tiyData.tData.websiteList.push(item);
        }
      );
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};

/**
 * 根据独立站查询有效客户
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const getCustomerByWebsite = (self, tiyData) => {
  self.$Spin.show();
  self.$apiPlugin.getCustomerByWebsite({
    "website": tiyData.tParams.website
  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      let userPage = response.result;
      tiyData.tPage.total = userPage.total;
      tiyData.tData.customerList.splice(0, tiyData.tData.customerList.length);
      userPage.list.forEach(
        function (item) {
          tiyData.tData.customerList.push(item);
        }
      );
      tiyData.tStatus.modalCustomer.isShow = true;
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};

/**
 * 从服务器请求客户详情
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const getCustomerDetail = (self, tiyData, id, state) => {
  self.$Spin.show();
  self.$apiPlugin.getCustomerInfoById({ "customerId": id, "state":state}).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      let result = response.result;
      tiyData.tData.customerInfo.id = result.id;
      tiyData.tData.customerInfo.name = result.name;
      tiyData.tData.customerInfo.channel = (result.channel == 1? "Instagram":"");
      tiyData.tData.customerInfo.avatar = result.avatar;
      tiyData.tData.customerInfo.followerCount = result.followerCount;
      tiyData.tData.customerInfo.followingCount = result.followingCount;
      tiyData.tData.customerInfo.externalUrl = result.externalUrl;
      tiyData.tData.customerInfo.fullName = result.fullName;
      tiyData.tData.customerInfo.email = result.email;
      tiyData.tData.customerInfo.contactUrl = result.contactUrl;
      tiyData.tData.customerInfo.externalEmail = result.externalEmail;
      tiyData.tData.customerInfo.state = result.state;
      tiyData.tData.customerInfo.stateTitle = self.targetCustomerStateDict.filter(item => item.value == result.state)[0].name;
      tiyData.tData.customerInfo.number = result.number;
      tiyData.tData.customerInfo.description = result.description;
      tiyData.tData.customerInfo.employeeId = result.employeeId;
      tiyData.tData.customerInfo.employeeName = result.employeeName;
      tiyData.tData.customerInfo.createtime = self.$tiyBJTime(result.createtime);
      tiyData.tData.customerInfo.opRecords = [];
      result.opRecords.forEach(function (item) { //设置操作记录
        let record = item;
        // record.employeeName = tiyData.tData.customerInfo.employeeName;
        tiyData.tData.customerInfo.opRecords.push(record);
      });
      tiyData.tStatus.modalCustomerInfo.isShow = true;
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};

/**
 * 设置用户状态
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const invalidCustomers = (self, tiyData) => {
  self.$Spin.show();
  let customerIds = [];
  tiyData.tParams.selectedUsers.forEach(function (item) {
    customerIds.push(item.id);
  });
  self.$apiPlugin.batchSetCustomerState({
    customerIds: customerIds,
    state: 300
  }).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      self.$Message.config("批量设置目标客户无效成功！");
      tiyData.tStatus.modalCustomer.isShow = false;
      searchService(self, tiyData);
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};

/**
 * 处理相同独立站的目标客户
 * @param self
 * @param tiyData
 * @returns {Array}
 */
export const clearSameWebsitesCustomers = (self, tiyData) => {
  self.$Spin.show();
  let urls = [];
  tiyData.tParams.selectedUrls.forEach(function (item) {
    urls.push(item.website);
  });
  self.$apiPlugin.clearSameWebsitesCustomers(urls).then(function (response) {
    self.$Spin.hide();
    if (response.success) {
      self.$Message.config("清理相同独立站的目标客户！");
      searchService(self, tiyData);
    } else {
      self.$Message.error(response.errorMessage);
    }
  });
};
